﻿using System.Collections.Generic;
using System.Configuration;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Reflection;
using DNBProject.DAO;
using DNBProject.Model;

namespace DNBProject.Common
{
    /// <summary>
    /// Contains all variant using on all module
    /// </summary>
    public class CommonInfo
    {
        /// <summary>
        /// Return the info of layer key in web.config
        /// </summary>
        public static IList<InfoLayerModel> LayerConfig { get { return ReadLayerConfig(); }}

        /// <summary>
        /// Read all info abount layer in web.config
        /// </summary>
        private static IList<InfoLayerModel> ReadLayerConfig()
        {
            var layerKeys = ConfigurationManager.AppSettings["LayerKey"].Split(',');
            var layerNames = ConfigurationManager.AppSettings["LayerName"].Split(',');
            var index = 0;
            return layerKeys.Select(key => new InfoLayerModel
                {
                    Key = key.Trim(),
                    ImageUrl = ConfigurationManager.AppSettings[key.Trim() + "UrlImage"],
                    Name = layerNames[index++].Trim(),
                    TocImageUrl = ConfigurationManager.AppSettings[key.Trim() + "TocUrlImage"]
                }).ToList();
        }

        /// <summary>
        /// Update the coordinate data to the geographic reference
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dataSet"></param>
        public static void UpdateDataLonLat<T>(ObjectSet<T> dataSet) where T : EntityObject
        {
            var isUpdate = false;

            foreach (var entityObject in dataSet)
            {
                //Get the extract type of entity
                var entityType = entityObject.GetType();

                //Get the data of old reference
                var valueX = entityType.InvokeMember(Contants.ColX, BindingFlags.GetProperty, null, entityObject, null);
                var valueY = entityType.InvokeMember(Contants.ColY, BindingFlags.GetProperty, null, entityObject, null);

                //Check if the GS reference of entity is existed
                if (((entityType.InvokeMember(Contants.ColLon, BindingFlags.GetProperty, null, entityObject, null) !=
                      null) &&
                     (entityType.InvokeMember(Contants.ColLat, BindingFlags.GetProperty, null, entityObject, null) !=
                      null)) ||
                    ((valueX == null) && (valueY == null)))
                {
                    continue;
                }

                //Convert to GS reference
                var p = Projection.MetersToDegrees(new PointD((double)valueX, (double)valueY));

                entityType.InvokeMember(Contants.ColLon, BindingFlags.SetProperty, null, entityObject,
                                        new object[] { p.X });
                entityType.InvokeMember(Contants.ColLat, BindingFlags.SetProperty, null, entityObject,
                                        new object[] { p.Y });
                isUpdate = true;
            }

            if (isUpdate)
            {
                DaoBase.Data.SaveChanges();
            }

        }
    }
}